package com.kiln.juxingqs.ui

import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import client.global.commlib.toast.toast
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.ZrPickOrderAdapter
import com.kiln.juxingqs.api.bean.ZrOrder
import com.kiln.juxingqs.databinding.ActivityZrPickorderBinding
import com.kiln.juxingqs.ui.vm.ZrPickOrderViewModel

/**
 * 摘牌记录
 */
class ZrPickOrderListActivity :
    BaseMiddleActivity<ZrPickOrderViewModel, ActivityZrPickorderBinding>() {

    var goods_id: String? = ""
    var goods_price: String? = ""
    var titleName: String? = ""
    var r_type: String? = ""
    var d_type: String? = ""
    var u_type: String? = ""
    var is_detail: Int?=0

    override fun getLayout(): Int {
        return R.layout.activity_zr_pickorder
    }

    val adapter: ZrPickOrderAdapter by lazy {
        ZrPickOrderAdapter(this, u_type, is_detail).apply {
            zhaiPaiFun = { orderId: String, num: String ->
                viewModel.holdZhaiPai(orderId, num)
            }

            cancelFun = {
                viewModel.holdCancel(it)
            }

        }
    }

    override fun initViewModel(): ZrPickOrderViewModel = createViewModel()

    override fun initView() {
        binding?.apply {
            event = this@ZrPickOrderListActivity
            vm = viewModel
            goods_id = intent.getStringExtra("goods_id")
            goods_price = intent.getStringExtra("goods_price")
            titleName = intent.getStringExtra("titleName")
            r_type = intent.getStringExtra("r_type")
            d_type = intent.getStringExtra("d_type")
            u_type = intent.getStringExtra("u_type")

        }


        binding.emptyLayout.setOnClickListener {
            viewModel.pageOne.value = 0
            viewModel.holdCall(goods_id)
        }
        val decoration = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        val dividerDrawable = resources.getDrawable(R.drawable.drawable_divider)
        decoration.setDrawable(dividerDrawable)
        binding.recyclerView.addItemDecoration(decoration)
        binding.recyclerView.adapter = adapter
        binding.recyclerView.onLoadMore = {
            if (adapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
                viewModel.holdCall(goods_id)
            }
        }
        binding.refreshLayout.setOnRefreshListener {
            viewModel.pageOne.value = 0
            adapter.hasMoreState.value = true
            viewModel.holdCall(goods_id)
        }
        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })

    }

    override fun initData() {
        viewModel.pageOne.value = 0
        viewModel.holdCall(goods_id)

    }

    override fun observerUI() {
        viewModel.callOne.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list =
                        it.data?.order_list as MutableList<ZrOrder.Order?>
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitList(list)
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 20) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })

        viewModel.zhaiPaiRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("已摘牌")
                    finish()
                }
                viewModel.zhaiPaiRet.value = null
            }
        })
        viewModel.cancelRet.observe(this, {
            it.let {
                if (it.isSuccess) {
                    toast("已撤销")
                    finish()
                }
                viewModel.zhaiPaiRet.value = null
            }
        })

    }


}